/*
 * @Author: linxun 
 * @Date: 2021-03-04 20:19:07 
 * @Last Modified by: linxun
 * @Last Modified time: 2021-07-23 15:27:50
 * @Description: 历史版本列表
 */

import React, { Fragment } from 'react';
import { useIntl, history } from 'umi';
import { Table, Divider, Popconfirm, Tooltip, Badge, Button } from 'antd';
import { getText } from '../util';
import moment from 'moment';
import { TIMEFORMAT, ADHUB_PREFIX } from '@/utils/const';
import { statusColorConfig } from './DataSetTable';
import { AuthzHOC } from '@apulis/authz';
import { SyncOutlined } from '@ant-design/icons';

import styles from '../index.less';

const SubTable = props => {
  const { formatMessage } = useIntl();
  const {
    data,
    loading,
    onSubTableChange,
    onDelete,
    onDetailClick,
    isPublic,
    listSourceType,
    onSegmentClick,
    createStatus,
    onDownLoadClick,
    onRefresh,
  } = props;

  const toSamples = record => {
    // const option = `${record.annotStatus !== 'labeling' && !isPublic ? 1 : 0}`;
    history.push({
      pathname: `/${isPublic ? 'public' : 'QIP'}/dataManage/dataSet/samples/${record.id}`,
      state: {
        record,
        option: 0,
        annotType: record.annotType,
      },
      query: {
        option: 0,
        annotType: record.annotType,
      }
    });
  };

  const columns = [{
    title: formatMessage({ id: 'dataSet.dataSet.table.version' }),
    dataIndex: 'version',
    render: (text, record) => <a onClick={() => onDetailClick(record)}>{text}</a>,
  }, {
    title: '公开数据',
    dataIndex: 'ownerType',
    render: text => text !== 'group' ? '是' : '否'
  }, {
    title: formatMessage({ id: 'dataSet.dataSet.table.sourceType' }),
    dataIndex: 'sourceType',
    render: text => getText(listSourceType, text),
  }, {
    title: formatMessage({ id: 'dataSet.dataSet.table.samplesNum' }),
    dataIndex: 'itemCount',
  }, {
    title: formatMessage({ id: 'dataSet.dataSet.table.label' }),
    dataIndex: 'labelList',
    render: text => text?.length > 3 ? <Tooltip title={text?.join('，')}>{text?.slice(0, 3)?.join('，')}，...</Tooltip> : text?.join('，'),
  }, {
    title: formatMessage({ id: 'dataSet.dataSet.table.createStatus' }),
    dataIndex: 'status',
    render: text => <Fragment><Badge color={statusColorConfig[text]} />{getText(createStatus, text)}</Fragment>,
  }, {
    title: formatMessage({ id: 'dataSet.table.createdAt' }),
    dataIndex: 'createdAt',
    // sorter: true,
    render: text => moment(text).format(TIMEFORMAT),
  }, {
    title: () => (
      <>
        {formatMessage({ id: 'dataSet.table.option' })}
        <Button
          onClick={() => onRefresh(data.pagination.datasetCode)}
          icon={<SyncOutlined />}
          style={{ marginLeft: 12 }}
        />
      </>
    ),
    dataIndex: 'option',
    fixed: 'right',
    align: 'right',
    render: (_, record) => (
      <Fragment>
        {/* <a onClick={() => toSamples(record)}>
          {formatMessage({ id: 'dataSet.dataSet.table.option.samples' })}
        </a>
        <Divider type='vertical' /> */}
        {record.status === 'success' &&
          <Fragment>
            <Tooltip title={record.sourceType === 'split_from_dataset' ? '数据来源为切分的数据集不能再次切分' : record.isSplited ? '该数据集已被切分过' : ''}>
              <a
                onClick={() => {
                  if (record.sourceType === 'split_from_dataset' || record.isSplited) return;
                  onSegmentClick(record);
                }}
                className={record.sourceType === 'split_from_dataset' || record.isSplited ? styles.disabled : ''}
              >数据切分</a>
            </Tooltip>
            <Divider type='vertical' />
            <a onClick={() => onDownLoadClick(record)}>
              {formatMessage({ id: 'dataSet.table.option.download' })}
            </a>
            <AuthzHOC anyOf={[
              [ADHUB_PREFIX + '/ad-hub/cv_datasets', 'delete'],
            ]}>
              <Divider type='vertical' />
            </AuthzHOC>
          </Fragment>
        }
        <AuthzHOC anyOf={[
          [ADHUB_PREFIX + '/ad-hub/cv_datasets', 'delete'],
        ]}>
          <Popconfirm
            title={<div style={{ maxWidth: 250 }}>删除已发布数据集可能会导致关联该数据集的任务运行失败，请确认是否删除？</div>}
            placement='topRight'
            onConfirm={() => onDelete(record, data.pagination.datasetCode)}
            okText='确认删除'
          >
            <a className={styles.dangerText}>
              {formatMessage({ id: 'dataSet.table.option.delete' })}
            </a>
          </Popconfirm>
        </AuthzHOC>
      </Fragment>
    )
  }];

  return (
    <Table
      dataSource={data.list}
      pagination={{
        ...data.pagination,
        showQuickJumper: true,
        showSizeChanger: true,
        pageSizeOptions: [5, 10, 20, 50, 100],
        showTotal: (total) => `${formatMessage({ id: 'dataSet.table.total' }, { total })}`,
        size: 'small',
      }}
      rowKey='id'
      columns={columns}
      onChange={onSubTableChange}
      loading={loading}
    />
  )
}

export default SubTable;